

%MACRO table_s2(in);

*****************************************************************************
* Program: table_s2			   						
* 																			
* Description: Produces Table S2
*						
* Last modified: Feb 8, 2016												
* Created by: Martin Wegman													
*****************************************************************************;

%let data = compare;

%let date = %sysfunc(date(),DATE7.);
%let name = CDDC Urine Table S2 &date..rtf;
%let file_name = &out_dir\&name;

data loc.&data;
	set loc.&in;

	if T_dis = . then miss_dis = 1;
	else miss_dis = 0;

run;

******************************************************
*************          Summary          **************
******************************************************
******************************************************;

*Create any summary variables if needed;
*Create/edit labels and formats if needed;

data comm;
	length	level 			$	150 
  			pvalue			$	17
  			ci c1 c2 		$	40;
  			
	nvar=23; _line=0.5; level="Readiness for change";		pvalue=''; 	output;
	nvar=29; _line=0.5; level="Social Support";				pvalue=''; 	output;
run;

proc format library = loc;
	value site_ 			1='CDDC' 
							2='Voluntary Treatment';
	value dm2_m_ 			1='Malay' 
							3='Indian' 
							2='Chinese and Other';
	value dm3_m_			0='No' 
							1='Yes';
	value dm4_m_ 			0='No' 
							1='Yes';
	value dm5_m_ 			1='Permanent' 
							2='Temporary';
	value du15_choice_m_ 	2='Heroin' 
							1='Other';
	value ny_ 				0='No' 
							1='Yes'
							.='Missing';
	value dast_cat_m_		0 = 'None Reported'
							2 = 'Low or Moderate'
							3 = 'Substantial'
							4 = 'Severe'
							.='Missing';
	value od_cat_ 			0='No' 
							1='Yes' 
							.='Missing';
	value cs01_craving_ 	00='00 (No craving whatsoever)' 
							01='01' 
							02='02' 
							03='03' 
							04='04' 
							05='05' 
							06='06' 
							07='07' 
							08='08' 
							09='09' 
							10='10 (I think about it all the time)' 
							98='Refuse to answer'
							.='Missing'; 
	value he03a_ 			0='HIV-negative' 
							1='HIV-positive' 
							2,3 ='Unknown'
							.='Missing';
	value miss_dis_			0="Known discharge date"
							1="Unknown discharge date";
run;

data  loc.&data.table;
	set  loc.&data;
	where site = 2;
	format miss_dis miss_dis_.;
	format site site_.;
	format dm2_m dm2_m_.;
	format dm3_m dm3_m_.;
	format dm4_m dm4_m_.;
	format dm5_m dm5_m_.;
	format DU15_choice_m DU15_choice_m_.;
	format rbd1 ny_.;
	format dast_cat_m dast_cat_m_.;
	format od_cat od_cat_.;
	format cs01_craving cs01_craving_.;
	format dt01 ny_.;
	format dt01b ny_.;
	format he01 ny_.;
	format he02 ny_.;
	format he03a he03a_.;
	format 	daily_heroin 
			lifetime_alcohol 
			lifetime_moreuse
			lifetime_other_opiates 
			lifetime_benzos 
			lifetime_stimulants ny_.;

	label dm0 = "Age (years)";
	label dm2_m = "Ethnicity, n(%)";
	label dm3_m = "Completed secondary school, n(%)";
	label dm4_m = "Married, n(%)";
	label dm5_m = "Previous housing type, n(%)";
	label IH01_Prison="Times imprisoned";
	label IH02_Lockup="Times in lockup/jail";
	label IH05_totaltimes="Times detained in CDDC*";
	label DU15_choice_m="Drug of choice, n(%)";
	label DU16_agefirstdrug="Age of first drug use (years)";
	label RBD1="Ever injected drugs, n(%)";
	label dast_cat_m="Drug use severity, n(%)";
	label od_cat="Opiate dependence, n(%)";
	label CS01_craving="Opiate cravings (0-10)";
	label so_rec_d="Recognition";
	label so_amb_d="Ambivalence";
	label so_ts_d="Taking Steps";
	label DT01="Ever received buprenorphine treatment*, n(%)";
	label DT01B="Recent buprenorphine treatment, n(%)";
	label HE01="Recent emergent/urgent care, n(%)";
	label HE02="Ever tested for HIV, n(%)";
	label HE03A="HIV test result, n(%)";	
	label ss_so="Significant other";
	label ss_fam="Family";
	label ss_fri="Friends";
	label du02b_heroin_years="Years of heroin use";
	label daily_heroin="Daily use of heroin before entering facility, n(%)";
	label lifetime_alcohol="Alcohol use (lifetime), n(%)";
	label lifetime_moreuse="Use of >1 drug at same time (lifetime), n(%)";
	label lifetime_other_opiates="Non-heroin opiate use (lifetime), n(%)";
	label lifetime_benzos="Benzodiazepine use (lifetime), n(%)";
	label lifetime_stimulants="Stimulant use (lifetime), n(%)";

run; 

%let master = loc.&data.table;
%let var_list = dm0 dm2_m dm3_m dm4_m dm5_m
				ih01_prison ih02_lockup ih05_totaltimes
				du16_agefirstdrug du15_choice_m
				du02b_heroin_years
				daily_heroin
				dast_cat_m cs01_craving
				rbd1
				lifetime_alcohol lifetime_other_opiates 
				lifetime_benzos lifetime_stimulants 
				lifetime_moreuse 
				dt01 dt01b
				so_rec_d so_amb_d so_ts_d
				he01 he02 he03a  
				ss_so ss_fam ss_fri;
				
				
%let var_type = c d d d d
				c c c
				c d
				c
				d
				d c
				d
				d d 
				d d
				d
				d d
				c c c
				d d d
				c c c;

%let var_dvar = ;*5 10 12 13 15 20 21 22 26 27 28;
%let var_dline = ;*2 2 2 2 2 2 2 2 2 2 2;

%let foot =;
%let foot2 =;

%table(	dsn = &master, /* input dataset */
 		var = &var_list, /* analysis variables */
 		type = &var_type, /* analysis variable types */
		total=n,
		pfoot=y,
		date = n,
		TITLEFNT= Times,
		headfnt = Times,
		bodyfnt = Times,
		MEDDEC = 0,
		RNGDEC  = 0,
		QDEC = 0,
		cstats   = median quartiles range,
		LABELWRAP = y,
		dvar = &var_dvar,
		dline = &var_dline,
		FOOT = &foot,
		FOOT2 = &foot2,
		TITLESZ = 10,
		BODYSZ  = 8,
		FOOTSZ  = 10,
		comments = comm,
		by = miss_dis, /* group variable */
 		outdoc = &file_name
); /* output RTF file name */

****************************************************************************;
****************************************************************************;
*********************   Remove old datasets               ******************;
****************************************************************************;
****************************************************************************;

proc datasets lib=loc nolist;       
	delete &data.table &data;  
run;
quit;

****************************************************************************;
****************************************************************************;
**************                END                ***************************;
****************************************************************************;
****************************************************************************;

%MEND table_s2;
